﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework;

namespace Simulatore_0._1
{
    public partial class MainView:Form
    {

        #region Variabili

        private ModelloFS2 modello;
        private Game1 game;
        public BackgroundWorker bw;
        #endregion Variabili

        public MainView ( Game1 game, ModelloFS2 modello )
        {
            this.modello=modello;
            this.game = game;
            InitializeComponent();
            bw = new BackgroundWorker();

            bw.WorkerReportsProgress = true;
            bw.WorkerSupportsCancellation = true;
            bw.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
            bw.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
            bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);
        }

        //setta il modello del main come modello della vista
        public void setModello ( ModelloFS2 modello )
        {
            this.modello = modello;
        }

        private void buttonStart_Click ( object sender, EventArgs e )
        {
            //se il thread non è in suspend....
            if(!(modello.is_suspend))
            {
                if(bw.IsBusy != true)
                {
                buttonStart.Enabled = false;
                buttonStop.Enabled = true;
                buttonSuspend.Enabled=true;
                buttonStart.Text="Start";
                    bw.RunWorkerAsync();               
                }           
            }
            else
            {
                //altrimenti faccio un resume del thread
                modello.ResumeEsecuzione(); 
                buttonStart.Enabled = false;
                buttonStop.Enabled = true;
                buttonSuspend.Enabled= true;

                buttonStart.Text="Start"; 
            }
        }

        private void esciToolStripMenuItem_Click ( object sender, EventArgs e )
        {
            Application.Exit();
        }

        //bottone stop
        private void button1_Click ( object sender, EventArgs e )
        { 
            if(bw.WorkerSupportsCancellation == true)
            {
                buttonStart.Text="Start";
                buttonStart.Enabled=true;
                buttonStop.Enabled = false;
                buttonSuspend.Enabled = false;
                tbProgress.Value=0;
                bw.CancelAsync();
            }
        }

        //bottone sospendi
        private void button1_Click_1 ( object sender, EventArgs e )
        {
            modello.SospendiCreazione();
            buttonStart.Text="Resume";
            buttonStart.Enabled = true;
            buttonSuspend.Enabled= false;
        }

        private void MainView_Load ( object sender, EventArgs e )
        {
            buttonStop.Enabled=false;
            buttonSuspend.Enabled=false;
            buttonStart.Enabled=true;
            reset_ScrollBar();

            codg.Checked=false;
            labelc.Enabled=false;
            trackBar2.Enabled=false;
            pictureBox2.Enabled=false;
            textBoxc.Enabled=false;
            aog.Enabled=false;
            tbProgress.Value=0;
            textBoxc.Text=Convert.ToString(trackBar2.Value);

            textBox11.Text=Convert.ToString(100 - Convert.ToInt32(textBox10.Text) - Convert.ToInt32(textBox9.Text));
        }

        private void codg_CheckedChanged ( object sender, EventArgs e )
        {
            if(codg.Checked==false)
            {
                labelc.Enabled=false;
                trackBar2.Enabled=false;
                pictureBox2.Enabled=false;
                textBoxc.Enabled=false;
                aog.Enabled=false;
            }
            else
            {
                labelc.Enabled=true;
                trackBar2.Enabled=true;
                pictureBox2.Enabled=true;
                textBoxc.Enabled=true;
                aog.Enabled=true;
            }
        }

        public void reset_ScrollBar ()
        {
            trackBarLeverageLeader.Value=10;
            textBoxlevLeader.Text=Convert.ToString(trackBarLeverageLeader.Value);
            trackBar1.Value=15;
            textBox3.Text=Convert.ToString(trackBar1.Value);
            trackBar3.Value=20;
            textBox7.Text=Convert.ToString(trackBar3.Value);
            trackBar2.Value=15;
            textBoxc.Text=Convert.ToString(trackBar2.Value);
            trackBar4.Value=50;
            textBox12.Text=Convert.ToString(trackBar4.Value);
            tbProgress.Value=0;
        }

        private void trackBar2_Scroll ( object sender, EventArgs e )
        {
            textBoxc.Text=Convert.ToString(trackBar2.Value);
        }

        private void trackBar1_Scroll ( object sender, EventArgs e )
        {
            textBox3.Text=Convert.ToString(trackBar1.Value);
        }

        private void textBox3_TextChanged ( object sender, EventArgs e )
        {
            textBox3.Text=Convert.ToString(trackBar1.Value);
        }

        private void textBox7_TextChanged ( object sender, EventArgs e )
        {
            textBox7.Text=Convert.ToString(trackBar3.Value);
        }

        private void trackBar3_Scroll ( object sender, EventArgs e )
        {
            textBox7.Text=Convert.ToString(trackBar3.Value);
        }

        private void trackBarLeverage_Scroll ( object sender, EventArgs e )
        {
            textBoxlevLeader.Text=Convert.ToString(trackBarLeverageLeader.Value);
        }

        private void textBoxc_TextChanged ( object sender, EventArgs e )
        {
            textBoxc.Text=Convert.ToString(trackBar2.Value);
        }

        private void textBox10_TextChanged ( object sender, EventArgs e )
        {
            if(textBox10.Text.CompareTo("")==0)
                textBox10.Text="0";
            if(Convert.ToInt32(textBox9.Text) + Convert.ToInt32(textBox10.Text)>100)
            {
                MessageBox.Show("Value can't not exceed "+Convert.ToString(100-Convert.ToInt32(textBox9.Text)), " ", MessageBoxButtons.OK, MessageBoxIcon.Error);
                textBox10.Text=Convert.ToString(100-Convert.ToInt32(textBox9.Text));
            }
            if(Convert.ToInt32(textBox10.Text)<0 || Convert.ToInt32(textBox10.Text)>100)
            {
                MessageBox.Show("Value must be between (0-100)!", " ", MessageBoxButtons.OK, MessageBoxIcon.Error);
                textBox10.Text="0";
            }

            textBox11.Text = Convert.ToString(100 - Convert.ToInt32(textBox9.Text) - Convert.ToInt32(textBox10.Text));
        }

        private void textBox9_TextChanged ( object sender, EventArgs e )
        {
            if(textBox9.Text.CompareTo("")==0)
                textBox9.Text="0";
            if(Convert.ToInt32(textBox9.Text) + Convert.ToInt32(textBox10.Text)>100)
            {
                MessageBox.Show("Value can't not exceed "+Convert.ToString(100-Convert.ToInt32(textBox10.Text)), " ", MessageBoxButtons.OK, MessageBoxIcon.Error);
                textBox9.Text=Convert.ToString(100-Convert.ToInt32(textBox10.Text));
            }
            if(Convert.ToInt32(textBox9.Text)<0 || Convert.ToInt32(textBox9.Text)>100)
            {
                MessageBox.Show("Value must be between (0-100)!", " ", MessageBoxButtons.OK, MessageBoxIcon.Error);
                textBox9.Text="0";
            }
            textBox11.Text = Convert.ToString(100 - Convert.ToInt32(textBox9.Text) - Convert.ToInt32(textBox10.Text));

        }

        private void trackBar4_Scroll ( object sender, EventArgs e )
        {
            textBox12.Text=Convert.ToString(trackBar4.Value);
        }

        private void textBox12_TextChanged ( object sender, EventArgs e )
        {
            textBox12.Text=Convert.ToString(trackBar4.Value);
        }

        private void textBox8_TextChanged ( object sender, EventArgs e )
        {
            if(textBox8.Text.CompareTo("")==0)
                textBox8.Text="0";
        }

        private void textBoxbf_TextChanged ( object sender, EventArgs e )
        {
            if(textBoxbf.Text.CompareTo("")==0)
                textBoxbf.Text="0";
        }

        private void textBoxnBancheLeader_TextChanged ( object sender, EventArgs e )
        {
            if(textBoxnBancheLeader.Text.CompareTo("")==0)
                textBoxnBancheLeader.Text="0";
        }

        private void MainView_FormClosed ( object sender, FormClosedEventArgs e )
        {
            Application.Exit();
        }

        private void impostazioniToolStripMenuItem_Click ( object sender, EventArgs e )
        {
            Login login = new Login(this);
            login.Show();
        }

        private void label20_Click ( object sender, EventArgs e )
        {
            if(checkBox2.Checked==false) {
                label20.Enabled=false;
            }
        }

        private void checkBox2_CheckedChanged ( object sender, EventArgs e )
        {
            if(checkBox2.Checked)
            {
                label20.Enabled=true;
                textBox15.Visible=true;
                textBox16.Visible=true;
                label21.Visible=true;
                label22.Visible=true;
            }
            else {
                label20.Enabled=false;
                textBox15.Visible=false;
                textBox16.Visible=false;
                label21.Visible=false;
                label22.Visible=false;
            }
        }

        private void textBox15_TextChanged ( object sender, EventArgs e )
        {
            if(textBox15.Text.CompareTo("")==0) textBox15.Text="0";
            if(Convert.ToInt32(textBox15.Text)>Convert.ToInt32(numStep.Text))
            {
                MessageBox.Show("Value can't not exceed" +numStep.Text+"(nStep)!", " ", MessageBoxButtons.OK, MessageBoxIcon.Error);
                textBox15.Text=numStep.Text;
            }

        }

        private void textBox16_TextChanged ( object sender, EventArgs e )
        {
            if(textBox16.Text.CompareTo("")==0) textBox16.Text="0";

            if(Convert.ToInt32(textBox16.Text)>Convert.ToInt32(numStep.Text))
            {
                MessageBox.Show("Value can't not exceed" +numStep.Text+"(nStep)!", " ", MessageBoxButtons.OK, MessageBoxIcon.Error);
                textBox16.Text=numStep.Text;
            }
        }

        private void backgroundWorker1_DoWork ( object sender, DoWorkEventArgs e )
        {
            BackgroundWorker worker = sender as BackgroundWorker;

            modello.avviaSimulazione(worker,e);       
        }

        private void backgroundWorker1_ProgressChanged ( object sender, ProgressChangedEventArgs e )
        {
            this.tbProgress.Value = (e.ProgressPercentage);
        }

        private void backgroundWorker1_RunWorkerCompleted ( object sender, RunWorkerCompletedEventArgs e )
        {
            if((e.Cancelled == true))
            {
                Console.WriteLine("CANCELLATO!");
            }
                
            else if(!(e.Error == null))
            {
                Console.WriteLine("ERRORE!!");
            }
            else //e.result
            {
                Console.WriteLine("FATTO!!");
                buttonStart.Text="Start";
                buttonStart.Enabled=true;
                buttonStop.Enabled = false;
                buttonSuspend.Enabled = false;
                reset_ScrollBar();
            }
        }
    }
}
